<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/


namespace Member\Model;
use Think\Model;
use \Org\Util\String;
    
class ShopCertificateModel extends Model {

	function UploadCertificate(){
		 
		$data['realname']=String::msubstr(I('post.realname'),0,120,C('S_CHARSET'),false);
		$data['usertype']=intval($_POST['usertype']) <3 && intval($_POST['usertype']) >0 ? intval($_POST['usertype']) : 1;

		if(empty($data['realname'])){
			$this->error='请输入名称!';
			return false;
		}
		
		$model=new \Upload\Model\FileModel();
		$file=$model->StartUpload(1);
		$fid=$file['id'];
		if(!empty($fid)){
			$data['uid']=$this->uid;
			$data['thumb']=$file['thumb'];
			$data['thumbcut']=$file['thumbcut'];
			$data['filepath']=$file['filepath'];
			$data['dateline']=NowTime();
			$data['status']=1;
			$data['fid']=$file['id'];
			
			$iscertificate=$this->token(false)->where(" uid=$this->uid ")->find();
			
			if(!empty($iscertificate['id'])){
				if ($this->token(false)->create($data)){
					if($this->token(false)->where(" uid=$this->uid ")->save()){
						$model->token(false)->where(" fid=$fid ")->save(array('isuse'=>1));
						if(!empty($iscertificate['fid'])){
							$model->DeleteUpload($iscertificate['fid']);
						}
						return true;
					}
				}
			}else{
				if ($this->token(false)->create($data)){
					if($id=$this->token(false)->add()){
						$model->token(false)->where(" fid=$fid ")->save(array('isuse'=>1));
						return $id;
					}
				}
			}
		}
		$this->error=$model->getError();
		return false;

	}
		
	function GetCertificate($uid=0){
		$uid=intval($uid);
		return $this->token(false)->where(" uid=%d ",$uid)->find();
	}
	
	function AgainCertificate(){
		$this->token(false)->where(" uid=$this->uid ")->save(array('status'=>0));
		M('Member')->token(false)->where(" uid=$this->uid ")->save(array('isshop'=>0));
		return true;
	}
}

?>